import java.util.Arrays;
import java.util.Comparator;

/**
 * @program: LeetCode
 * @description: LeetCode : 435. 无重叠区间
 * @author: WXY
 * @create: 2023-01-24 09:03
 * @Version 1.0
 **/
public class Num435_eraseOverlapIntervals {
    public static int eraseOverlapIntervals(int[][] intervals) {

//        Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
        Arrays.sort(intervals, (a,b)-> a[1]-b[1]);
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[1] == o2[1]) return o1[0] - o2[0];
                return o1[1] - o2[1];
            }
        });
        int count = 1;
        int end = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >= end) {
                count++;
                end = intervals[i][1];
            }
        }
        return intervals.length -count;
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 2}, {2, 3}, {3, 4}, {1, 3}};
        int i = eraseOverlapIntervals(arr);
        System.out.println(i);
    }
}
